<?php if(navigation::getAdminAction() === 'create' || navigation::getAdminAction() === 'update' ): ?>
<?php

if(navigation::getAdminModeleId()>0){
    $galerie = new modele_galerie(navigation::getAdminModeleId());
    
    if(isset($_POST) && isset($_POST['save_order'])){
        foreach($_POST as $var=>$value){
            if(substr($var,0,6)==='order_'){
                list($s, $idp) = explode('_',$var,2);
                $galerie->changeOrder($idp,$value);
            }
        }
    }
    
    if(isset($_POST) && isset($_POST['submit'])){
        $pl = (isset($_GET['pl'])&&$_GET['pl']!=='')?'&pl='.$_GET['pl']:'';
        header('location: ?template=galerie&action=update&modele=galerie&modele_id='.navigation::getAdminModeleId().$pl);
        ob_end_flush();
    }
    
    $data = $galerie->getDatas();
    $data['permalien'] = $galerie->getPermalien()->getCleanedLien(false);
    $action = 'update';
}else{
    $action = 'create';
    $data = array();
    if(isset($_GET['pl'])){
        $data['permalien'] = str_replace(modele_permalien::getExtensionByModele('galerie'), '', $_GET['pl']);
        $data['title'] = 'Nouvelle galerie';
    }
}


?>
<div class="columns thirteen">
    <h1><a class="pwe-ui-btn" href="?template=galerie"><span class="ui-icon ui-icon ui-icon-arrowthick-1-w"></span></a> Modifier/créer une galerie</h1>
    <hr style="margin: 5px 0 15px 0;">
</div>
<form style="margin: 12px 0;" method="post" enctype="multipart/form-data" action="?template=galerie&action=<?php echo $action; ?>&modele=galerie&do=1&modele_id=<?php echo (isset($data['id']))?$data['id']:0; ?><?php echo (isset($_GET['pl'])&&$_GET['pl']!=='')?'&pl='.$_GET['pl']:''; ?>">
    <div class="columns nine">
        <div align="right">
            <input type="submit" name="submit" value="Enregistrer" class="pwe-ui-btn"/>
        </div>
        <?php echo $this->getInputHtml('Titre', 'modele_title', (isset($data['title']))?$data['title']:'', 'galerie', (isset($data['id']))?$data['id']:0, array('class_input'=>'titreModele','actions'=>array('onblur'=>"javascript:generatePermalien($(this).val());"))); ?>
        <?php if(isset($_GET['pl'])&& $_GET['pl']!==''): ?>
        <input type="hidden" name="modele_lien" value="<?php echo (isset($data['permalien']))?$data['permalien']:''; ?>" />
        <?php else: ?>
        <?php echo $this->getInputHtml('Permalien', 'modele_lien', (isset($data['permalien']))?$data['permalien']:'', 'galerie', (isset($data['id']))?$data['id']:0, array('prefix'=>PWE::getBaseURL(),'sufix'=>modele_permalien::getExtensionByModele('galerie'),'id_input'=>'permalien','actions'=>array('onblur'=>"javascript:validPermalien($(this).val());"))); ?>
        <?php endif; ?>
        <h3>liste des médias</h3>
        <?php if(isset($data['id'])&&$data['id']>0): ?>
        <div align="right" style="margin-top: -25px;">
            <a class="pwe-ui-btn" href="javascript:openAddMedia(<?php echo (isset($data['id']))?$data['id']:0; ?>,'galerie',1280,1024);">Ajouter</a>
            <br/>
            <br/>
        </div>
        <?php endif; ?>
        <table id="liste_media_<?php echo (isset($data['id']))?$data['id']:0; ?>" class="liste ui-widget" width="100%">
            <thead class="ui-widget-header">
                <tr>
                    <th>Titre</th>
                    <th width="150">Aperçu</th>
                    <th>Légende</th>
                </tr>
            </thead>
            <tbody class="liste-items-menu">
                <?php if(isset($data['medias']) && is_array($data['medias']) && count($data['medias']>0)): ?>
                <?php foreach($data['medias'] as $media): ?>
                <tr id="media_<?php echo $media['id']; ?>" class="ui-widget-content" style="cursor: move;">
                    <td>
                        <span><?php echo $media['title']; ?></span><br/>
                        <a href="javascript:openUpdateMedia(<?php echo $data['id']; ?>,<?php echo $media['id']; ?>,'galerie',1280,1024);">Modifier</a> | 
                        <a href="javascript:removeMedia(<?php echo $data['id']; ?>,<?php echo $media['id']; ?>,'galerie');" class="confirmAction" title="Supprimer le media '<?php echo $media['title']; ?>' de la galerie '<?php echo $data['title']; ?>'">Supprimer</a>
                        <input type="hidden" class="orderValue" name="order_<?php echo $media['id']; ?>" value="<?php echo $media['order']; ?>" />
                    </td>
                    <?php
                        $oMedia = new modele_media($media['id']);
                        $thumb = ($oMedia->getMediaType()=='image')?$oMedia->getUrl('../'):PWE::getBaseURL().'admin/css/gfx/logoPdf.png';
                    ?>
                    <td><img src="<?php echo $thumb; ?>" width="120px" title="<?php echo $media['title']; ?>" alt="<?php echo $media['title']; ?>" /></td>
                    <td><?php echo $media['legende']; ?></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
            <tfoot></tfoot>
        </table>
        <?php if(isset($data['id'])&&$data['id']>0): ?>
        <div align="right">
            <br/>
            (modifier l'ordre en déplaçant les lignes par glisser/déposer) <input class="pwe-ui-btn" type="submit" name="save_order" value="enregistrer l'ordre" />
        </div>
        <?php else: ?>
        <div align="center">
            <br/>
            <p><b>nota : Enregistrer la galerie avant de pouvoir ajouter un média.</b></p>
        </div>
        <?php endif; ?>
        <?php include('inc/seoManager.inc.php'); ?>
        <?php include('inc/redirectionManager.inc.php'); ?>
        <div align="right">
            <input type="submit" name="submit" value="Enregistrer" class="pwe-ui-btn"/>
        </div>
    </div>
    <div class="columns four">
        <input type="hidden" name="modele_template" value="sidebar-galerie" />
        <h3>Appartient à la rubrique :</h3>
        <select name="modele_category_id">
            <option value=""></option>
            <?php foreach(modele_category::getPrimaryListe() as $category): ?>
            <?php
                if( $data['category_id'] == $category->getId()){
                    $selected = ' selected="selected"';
                }else{
                    $selected = '';
                }
            ?>
            <option value="<?php echo $category->getId(); ?>"<?php echo $selected; ?>><?php echo $category->getTitle(); ?></option>

                <?php if($category->haveChild()): ?>
                <?php foreach($category->getChild() as $sCategory): ?>
                <?php
                    if( $data['category_id'] == $sCategory->getId()){
                        $selected = ' selected="selected"';
                    }else{
                        $selected = '';
                    }
                ?>
                <option value="<?php echo $sCategory->getId(); ?>"<?php echo $selected; ?>> - <?php echo $sCategory->getTitle(); ?></option>

                    <?php if($sCategory->haveChild()): ?>
                    <?php foreach($sCategory->getChild() as $ssCategory): ?>
                    <?php
                        if( $data['category_id'] == $ssCategory->getId()){
                            $selected = ' selected="selected"';
                        }else{
                            $selected = '';
                        }
                    ?>
                    <option value="<?php echo $ssCategory->getId(); ?>"<?php echo $selected; ?>> - - <?php echo $ssCategory->getTitle(); ?></option>
                    <?php endforeach; ?>
                    <?php endif; ?>

                <?php endforeach; ?>
                <?php endif; ?>

            <?php endforeach;?>
        </select>
        <input type="hidden" name="modele_id" value="<?php echo (isset($data['id']))?$data['id']:0; ?>" />
    </div>
</form>
<?php else: ?>
<div class="columns thirteen">
    <h1>Liste des galeries</h1>
    <hr style="margin: 5px 0 25px 0;">
    <div align="right">
        <a class="pwe-ui-btn" href="?template=galerie&modele=galerie&action=create">Créer une galerie</a>
    </div><br/>
    <table class="liste ui-widget" width="100%">
        <thead class="ui-widget-header">
            <tr>
                <th>Titre</th>
                <th>Rubrique</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach(modele_galerie::getListe(0) as $galerie): ?>
            <tr class="ui-widget-content">
                <td>
                    <span><?php echo $galerie->getTitle(); ?></span><br/>
                </td>
                <td><?php echo str_replace(PWE::getBaseURL(), '', $galerie->getCategory()->getUrl()); ?></td>
                <td>
                    <a class="pwe-ui-btn" href="?template=galerie&modele=galerie&action=update&modele_id=<?php echo $galerie->getId(); ?>" title="Modifier"><span class="ui-icon ui-icon-pencil"></span></a>
                    <a href="?template=galerie&modele=galerie&action=delete&do=1&modele_id=<?php echo $galerie->getId(); ?>" class="confirmAction pwe-ui-btn" title="Supprimer la galerie '<?php echo $galerie->getTitle(); ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot></tfoot>
    </table>
</div>
<?php endif; ?>